package demo.service;

import demo.model.Stock;
import demo.repo.StockRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实现StockRepo功能，为控制器提供服务的的业务逻辑类
 */
@Service
public class StockService {
    final private StockRepo stockRepo;

    @Autowired
    public StockService(StockRepo stockRepo){
        this.stockRepo = stockRepo;
    }
    //根据name查找库存
    public List<Stock> findByName(String name){
        return stockRepo.findByName(name);
    }
    //返回所有库存
    public List<Stock> findAll(){
        return stockRepo.findAll();
    }
    //删除id为10的库存
    public void delete(){
        Stock delStock = stockRepo.getOne(10);
        stockRepo.delete(delStock);
    }
    //根据id删除库存
    public void deleteStockById(int id){
        stockRepo.deleteById(id);
    }
    //插入库存
    public Stock insertStock(){
        Stock stock = new Stock();
        stock.setId(10);
        stock.setName("hahaha");
        stock.setDescription("good");
        stock.setNum(48);
        return stockRepo.save(stock);
    }
    //修改库存
    public Stock updateStock(){
        Stock stock = stockRepo.getOne(10);
        stock.setNum(50);
        return stockRepo.save(stock);
    }


}
